package com.shigu.works.controllers;

import com.opentae.data.mall.beans.RolePermission;
import com.shigu.admin.manager.actions.MainAction;
import com.shigu.admin.manager.vo.Nav;
import com.shigu.component.enums.SessionEnum;
import com.shigu.component.shiro.AuthorityUser;
import com.shigu.component.shiro.CaptchaUsernamePasswordToken;
import com.shigu.exceptions.BaohuoException;
import com.shigu.works.bo.LgabhRequest;
import com.shigu.works.exceptions.WorkBaseException;
import com.shigu.works.vo.BaseResponse;
import com.shigu.works.vo.UserinfoResponse;
import com.shigu.works.vo.UserinfoVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 网页用户登陆
 */
@Controller
@RequestMapping("/adminjson/")
public class WebUserLoginController extends MainAction {
    /**
     * 登陆
     * @param bo
     * @return
     */
    @RequestMapping("lgabh")
    @ResponseBody
    public BaseResponse lgabh(@Valid LgabhRequest bo, BindingResult result, HttpServletRequest request) throws WorkBaseException {
        if (result.hasErrors()) {
            throw new WorkBaseException(result.getFieldError().getDefaultMessage());
        }

        BaseResponse res=new BaseResponse();
        Subject currentUser = SecurityUtils.getSubject();

        if (currentUser.isAuthenticated()){//如果已经登陆，去登陆页面
            return res.success();
        }else if(bo.getUsername()!=null&&bo.getPassword()!=null){
            CaptchaUsernamePasswordToken token = new CaptchaUsernamePasswordToken(
                    bo.getUsername(), bo.getPassword(),request.getHeader("Host"), false, "", "");
//            token.setRememberMe(true);
            try {
                currentUser.login(token);
                //登陆成功
                return res.success();
            } catch (AuthenticationException e) {
                //登陆失败
                token.clear();
                String msg="账号或密码错误";
                return res.error(msg);
            }
        }
        return res.error("登陆失败");
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping("logout")
    @ResponseBody
    public BaseResponse logout(){
        SecurityUtils.getSubject().logout();
        return new BaseResponse().success();
    }

    @RequestMapping("userinfo")
    @ResponseBody
    public UserinfoResponse userinfo(HttpSession session){
        AuthorityUser user= (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        UserinfoResponse res=new UserinfoResponse();
        res.setAuth("admin");
        UserinfoVO vo=new UserinfoVO();
        vo.setUsername(user.getNick());
        vo.setOemLogoTxt("报货系统");
        vo.setMenus(super.navs());
        res.setInfo(vo);
        return res.success();
    }


}
